/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.action;

import com.myapp.facade.Facade;
import com.myapp.form.ArquivoForm;
import com.myapp.pojo.Arquivos;
import com.myapp.pojo.Usuario;
import com.sun.jersey.spi.inject.Errors.ErrorMessage;
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMessages;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author samuel
 */
public class ArquivoAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String CAMINHO_SERVIDOR = Facade.getInstance().buscarConfiguracao().getCaminho();

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ArquivoForm myForm = (ArquivoForm) form;

        if (!myForm.validate(mapping, request).isEmpty()) {
            return mapping.findForward(FAILURE);
        }
        //Grava o arquivo passado no servidor
        FormFile myFile = myForm.getMyFile();
        String fileName = myFile.getFileName();
        int fileSize = myFile.getFileSize();
        byte[] fileData = myFile.getFileData();

        if (Facade.getInstance().existeArquivo(CAMINHO_SERVIDOR + fileName)) {
            ActionErrors actionErrors = new ActionErrors();
            actionErrors.add("errors.arquivoexistente", new ActionMessage("errors.arquivoexistente"));
            return mapping.findForward(FAILURE);
        }
        File file = new File(CAMINHO_SERVIDOR + fileName);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(fileData);
        fileOutputStream.close();

        request.getSession().setAttribute("usuario", Facade.getInstance().buscaUsuario(1));
        Facade.getInstance().inserirArquivo(new Arquivos((Usuario) request.getSession().getAttribute("usuario"), CAMINHO_SERVIDOR + fileName, fileName));

        //adiciona a mensagem que sera mostrada em caso de sucesso no upload do arquivo
        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.arquivo"));
        saveMessages(request, messages);

        return mapping.findForward(SUCCESS);
    }
}
